package com.example.sprintbootarithmetic.sort.SelectSort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * 选择排序:每次选出最小的，然后与他交换
 *
 * @param
 * @description:ѡ������
 * @return: null
 * @author: daixingyu
 * @date: 2021-06-24  11:34
 */
public class SelectSort {
    /**
     * ��������
     *
     * @param array
     * @description:�ҵ�ѡ������
     * @return: int
     * @author: daixingyu
     * @date: 2021-06-23  11:35
     */
    public static int[] selectsort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                int max = array[i];
                if (array[j] > max) {
                    max = array[j];
                    array[j] = array[i];
                    array[i] = max;
                }
            }
        }
        return array;
    }

    /**
     * ��������
     *
     * @param arrays
     * @description:��ʦ��ѡ������
     * @return: int
     * @author: daixingyu
     * @date: 2021-06-23  11:34
     */
    public static int[] selectSortarray(int[] arrays) {
        for (int j = 0; j < arrays.length; j++) {
            int min = arrays[j];
            int minIndex = j;
            for (int i = j + 1; i < arrays.length; i++) {
                if (min < arrays[i]) {
                    min = arrays[i];
                    minIndex = i;
                }
            }
            if (minIndex != j) {
                arrays[minIndex] = arrays[j];
                arrays[j] = min;
            }
        }
        return arrays;
    }

    public static void main(String[] args) {
        int[] f = {1, 3, 2};
        Date data1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:MM:ss");
        simpleDateFormat.format(data1);
        String datestr1 = simpleDateFormat.format(data1);
        System.out.println("����ǰ��ʱ��Ϊ" + datestr1);
        selectSortarray(f);
        System.out.println(Arrays.toString(f));
        Date data2 = new Date();
        System.out.println("������ʱ��Ϊ" + simpleDateFormat.format(data2));
        array(f);
        System.out.println(Arrays.toString(f));

    }

    public static int[] array(int[] array) {
        int min = 0;//�����С��ֵ
        int maxindex = 0;//�����С������
        for (int i = 0; i < array.length; i++) {
            min = array[i];//

            for (int j = i + 1; j < array.length; j++) {
                maxindex = j;
                if (array[j] < min) {
                    min = array[i];
                    /* maxindex = j;*/
                }
            }
            if (maxindex != array.length - 1) {
                array[maxindex] = array[i];
                array[i] = min;
            }
        }
        return array;
    }
}
